package com.email;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.*;
import java.util.Date;
import java.util.Properties;

/**
 * @Auther: desk-----xuzhiliang
 * @Date: 2019/8/2 12:05
 * @Description:
 */
//发送邮件
public class MailDemo {
    public static void main(String[] args) {
        String to = "nuonuonuooh01@gmail.com";
        String from = "654096990@qq.com";
        String subject = "test";
        String body = "mail-body";
        try {
            /**
             *@Description 创建一个email格式的文件并保存在d盘
             *@Param  * @param args
             *@Return void
             *@Author desk-----xuzhiliang
             *@Date 2019/8/2
             *@Time 14:19
             */
            String smtpHost = "smtp.qq.com";
            Properties props = new Properties();
            props.setProperty("mail.transport.protocol", "smtp"); // 使用的协议（JavaMail规范要求）
            props.setProperty("mail.smtp.host", smtpHost); // 发件人的邮箱的 SMTP服务器地址
            props.setProperty("mail.smtp.auth", "true"); // 请求认证，参数名称与具体实现有关
            //创建session对象
            Session session = Session.getDefaultInstance(props);
            //创建mimemessage实例对象
            MimeMessage mimeMessage = new MimeMessage(session);
            //设置发件人
            mimeMessage.setFrom(new InternetAddress(from));
            //设置收件人
            mimeMessage.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
            //设置发送时间
            mimeMessage.setSentDate(new Date());
            mimeMessage.setSubject(subject);
            mimeMessage.setText(body);
            mimeMessage.saveChanges();
            File file = new File("d://test.eml");
            //写入磁盘
            mimeMessage.writeTo(new FileOutputStream(file));
            //输出详细的发送过程
            session.setDebug(true);
            // 获取Transport对象
            Transport transport = session.getTransport("smtp");
            // 第2个参数需要填写的是QQ邮箱的SMTP的授权码
            String password = "ppjknkyrsfqkbebj";
            transport.connect(from, password);
            // 发送，message.getAllRecipients() 获取到的是在创建邮件对象时添加的所有收件人, 抄送人, 密送人
            transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());
            transport.close();

            InputStream inputStream = new FileInputStream(file);
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String line = bufferedReader.readLine();
            while (line != null) {
                System.out.println(line);
                line = bufferedReader.readLine();
            }
        } catch (MessagingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            System.out.println("文件没有找到");
        } catch (IOException e) {
            System.out.println("读写错误");
        }
    }
}
